#include "b_rook.h"

#include "board.h"
#include "gameengine.h"
#include <iostream>

using namespace std;

Rook::Rook() {
}

Rook::Rook(int r, int c , Color color) {
	row = r;
	col = c;
	pColor = color;
	setImage();
}

Rook::~Rook() {

}


void Rook::setImage() {
	if(pColor==white) {
		image = W_ROOK;
	} else {
		image = B_ROOK;
	}
}

void Rook::GetAllPossible(std::vector <Cell>  & moves) {
	bool oneStop = false;
	bool twoStop = false;
	for( int i = 1 ; i <= 7 ; i++) {

		if(MyFunctions::isInRange(row+i,col) && !oneStop) {
			moves.push_back(Cell(row+i,col));
			if(myBoard->hasEnemy(pColor,row+i,col)) {
				oneStop = true;
			}
			if(myBoard->hasTeamate(pColor,row+i,col)) {
				moves.pop_back();
				oneStop = true;
			}

		}

		if(MyFunctions::isInRange(row-i,col) && !twoStop) {
			moves.push_back(Cell(row-i,col));
			if(myBoard->hasEnemy(pColor,row-i,col)) {
				twoStop = true;
			}
			if(myBoard->hasTeamate(pColor,row-i,col)) {
				moves.pop_back();
				twoStop = true;
			}
		}
	}
	SearchMoreMoves(moves);

}

void Rook::SearchMoreMoves(std::vector <Cell> & moves) {
	bool oneStop = false;
	bool twoStop = false;
	for ( int i = 1 ; i <= 7 ; i++) {
		if(MyFunctions::isInRange(row,col-i) && !oneStop) {
			moves.push_back(Cell(row,col-i));
			if(myBoard->hasEnemy(pColor,row,col-i)) {
				oneStop = true;
			}
			if(myBoard->hasTeamate(pColor,row,col-i)) {
				moves.pop_back();
				oneStop = true;
			}
		}

		if(MyFunctions::isInRange(row,col+i) && !twoStop) {
			moves.push_back(Cell(row,col+i));
			if(myBoard->hasEnemy(pColor,row,col+i)) {
				twoStop = true;
			}
			if(myBoard->hasTeamate(pColor,row,col+i)) {
				moves.pop_back();
				twoStop = true;
			}
		}
	}

}

bool Rook::isPiece() {
	return true;
}


void Rook::setCell(int r, int c) {
	row = r;
	col = c;
}

Color Rook::GetColor() {
	return pColor;
}

ImageName Rook::GetImage() {
	return image;
}


bool Rook::Test(std::ostream &os) {
	bool success = true;
	if (!Rook::TestMoves(std::cout)) success = false;
	if (!Rook::TestCheck(std::cout)) success = false;
	return success;
}

bool Rook::TestMoves(std::ostream &os) {

	bool success = true;

	GameEngine testGame;
	testGame.gameStarted = true;
	testGame.myBoard.piecesOnBoard.push_back(new Rook(0,0,white));
	testGame.myBoard.piecesOnBoard.push_back(new Rook(1,1,white));
	testGame.myBoard.piecesOnBoard.push_back(new Rook(2,2,white));
	testGame.myBoard.piecesOnBoard.push_back(new Rook(3,3,white));
	testGame.myBoard.piecesOnBoard.push_back(new Rook(6,6,white));

	testGame.myBoard.piecesOnBoard.push_back(new Pawn(5,2,black));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(6,1,black));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(1,3,black));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(3,1,white));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(5,3,white));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(2,6,white));

	testGame.turn = white;
	ChessPiece * p1 = testGame.myBoard.piecesOnBoard[1-1];
	ChessPiece * p2 = testGame.myBoard.piecesOnBoard[2-1];
	ChessPiece * p3 = testGame.myBoard.piecesOnBoard[3-1];
	ChessPiece * p4 = testGame.myBoard.piecesOnBoard[4-1];
	ChessPiece * p5 = testGame.myBoard.piecesOnBoard[5-1];

	p1->setBoard(&testGame.myBoard);
	p2->setBoard(&testGame.myBoard);
	p3->setBoard(&testGame.myBoard);
	p4->setBoard(&testGame.myBoard);
	p5->setBoard(&testGame.myBoard);

	std::vector<Cell> moves1;
	std::vector<Cell> moves2;
	std::vector<Cell> moves3;
	std::vector<Cell> moves4;
	std::vector<Cell> moves5;

	p1->GetAllPossible(moves1);
	p2->GetAllPossible(moves2);
	p3->GetAllPossible(moves3);
	p4->GetAllPossible(moves4);
	p5->GetAllPossible(moves5);

	std::vector<Cell>answer1;
	std::vector<Cell>answer2;
	std::vector<Cell>answer3;
	std::vector<Cell>answer4;
	std::vector<Cell>answer5;

	for(int i = 1 ; i <= 7 ; i++) {
		answer1.push_back(Cell(0,i));
		answer1.push_back(Cell(i,0));
	}

	answer2.push_back(Cell(0,1));
	answer2.push_back(Cell(1,0));
	answer2.push_back(Cell(2,1));
	answer2.push_back(Cell(1,2));
	answer2.push_back(Cell(1,3));

	answer3.push_back(Cell(0,2));
	answer3.push_back(Cell(1,2));
	answer3.push_back(Cell(2,0));
	answer3.push_back(Cell(2,1));
	answer3.push_back(Cell(2,3));
	answer3.push_back(Cell(2,4));
	answer3.push_back(Cell(2,5));
	answer3.push_back(Cell(3,2));
	answer3.push_back(Cell(4,2));
	answer3.push_back(Cell(5,2));

	answer4.push_back(Cell(3,2));
	answer4.push_back(Cell(3,4));
	answer4.push_back(Cell(3,5));
	answer4.push_back(Cell(3,6));
	answer4.push_back(Cell(3,7));
	answer4.push_back(Cell(1,3));
	answer4.push_back(Cell(2,3));
	answer4.push_back(Cell(4,3));

	answer5.push_back(Cell(6,1));
	answer5.push_back(Cell(6,2));
	answer5.push_back(Cell(6,3));
	answer5.push_back(Cell(6,4));
	answer5.push_back(Cell(6,5));
	answer5.push_back(Cell(6,7));
	answer5.push_back(Cell(3,6));
	answer5.push_back(Cell(4,6));
	answer5.push_back(Cell(5,6));
	answer5.push_back(Cell(7,6));

	TEST(Pawn::CheckCellVectors(moves1,answer1));
	TEST(Pawn::CheckCellVectors(moves2,answer2));
	TEST(Pawn::CheckCellVectors(moves3,answer3));
	TEST(Pawn::CheckCellVectors(moves4,answer4));
	TEST(Pawn::CheckCellVectors(moves5,answer5));



	return success;
}

bool Rook::TestCheck(std::ostream &os) {

	bool success = true;
	GameEngine testGame;
	testGame.gameStarted = true;
	testGame.turn = white;

	testGame.myBoard.piecesOnBoard.push_back(new Rook(5,2,white));
	testGame.myBoard.piecesOnBoard.push_back(new Rook(5,3,white));
	testGame.myBoard.piecesOnBoard.push_back(new King(6,3,white));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(3,0,black));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(2,3,black));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(4,4,black));

	ChessPiece * p1 = testGame.myBoard.piecesOnBoard[0];
	ChessPiece * p2 = testGame.myBoard.piecesOnBoard[1];



	std::vector<ChessPiece *>::iterator it;
	for ( it = testGame.myBoard.piecesOnBoard.begin() ;
	        it < testGame.myBoard.piecesOnBoard.end() ; it++) {
		ChessPiece * p = *it;
		p->setBoard(&testGame.myBoard);
	}

	std::vector<Cell> moves1;
	std::vector<Cell> moves2;

	testGame.FindMoves(moves1,p1);
	testGame.FindMoves(moves2,p2);

	std::vector<Cell> answer1;
	std::vector<Cell> answer2;

	answer2.push_back(Cell(4,3));
	answer2.push_back(Cell(3,3));
	answer2.push_back(Cell(2,3));

	TEST(Pawn::CheckCellVectors(moves1,answer1));
	TEST(Pawn::CheckCellVectors(moves2,answer2));

	return success;
}

int Rook::getValue()
{
    return 5;
}


/*
  TEST MOVES------------------------

  Capital = black
  lower = white
  p = pawn
  Q = queen
  r = rook

    0 1 2 3 4 5 6 7
  0 r . . . . . . .  TEST 1(14)
  1 . r . P . . . .  TEST 2(5)
  2 . . r . . . p .  TEST 3(10)
  3 . p . r . . . .  TEST 4(8)
  4 . . . . . . . .
  5 . . P p . . . .
  6 . P . . . . r .  TEST 5(10)
  7 . . . . . . . .

  TEST CHECK------------------------

  Capital = black
  lower = white
  p = pawn
  q = queen
  k = king

    0 1 2 3 4 5 6 7
  0 . . . . . . . .
  1 . . . . . . . .
  2 . . . Q . . . .
  3 Q . . . . . . .
  4 . . . . Q . . .
  5 . . r r . . . . TEST 1 | TEST 2
  6 . . . k . . . .
  7 . . . . . . . .

    0 1 2 3 4 5 6 7
  0 . . . . . . . .
  1 . . . . . . . .
  2 . . . . . . . .
  3 . . . . . . . .
  4 . . . . . . . .
  5 . . . . . . . .
  6 . . . . . . . .
  7 . . . . . . . .


  */
